package cn.yejinwen.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import cn.chenjifeng.model.OrderModel;
import cn.common.dao.BaseDao;
import cn.common.tool.Query;
import cn.common.tool.dele;
import cn.common.tool.seach;
import cn.xiezonjie.model.Menu;
import cn.yejinwen.model.Admin_id;
import cn.yejinwen.model.Carts;
import cn.yejinwen.model.Desk_id;
import cn.yejinwen.model.Menu_id;
import cn.yejinwen.model.Order;
import cn.yejinwen.dao.OrderCartDao;

@SuppressWarnings("unused")
public class OrderCartImpl extends  BaseDao implements OrderCartDao {
	

	
	

	//添加数据到订餐车-orders表
	public int AddCart(Menu_id m_id,Desk_id t_id,Admin_id a_id)  throws SQLException{
		
		String sql="exec  AddOrderr "
					+m_id.getM_id()+","
					+t_id.getT_id()+","
					+a_id.getA_id();
	
		System.out.println("插入时执行的存储过程 : "+sql);
	
		dele d = new dele();
		return d.dele(sql);		
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public ResultSet reAddCart( Desk_id t_id) throws SQLException {   //查询餐桌id
		
		try{
	    String sql="select * from orders where t_id=?";
	    System.out.println("这是查询是否存在餐桌id"+sql);
		
		Connection conn;
		boolean flag;
		conn = this.getConn();
		ResultSet rs;
		PreparedStatement pstmt=conn.prepareStatement(sql);
		pstmt.setObject(1,t_id);
		
	    return pstmt.executeQuery();  			   
		}catch(Exception e){
			e.printStackTrace();
		}		
		return null;
	}

	public String DongTaiDate()
	{
		Calendar cal = Calendar.getInstance();
	    Date date = cal.getTime();
	    return  (new SimpleDateFormat("ssSSS").format(date));
	}

	
	/*-----这是二次插入菜品需要的id查询---------*/
	public ResultSet reAddMenu(Menu_id m_id) throws SQLException {//查询菜品id
	
		try{
	    String sql="select * from orders where m_id=?";
	    System.out.println("这是查询菜品id"+sql);		
		Connection conn;
		boolean flag;
		conn = this.getConn();
		ResultSet rs;
		PreparedStatement pstmt=conn.prepareStatement(sql);
		pstmt.setObject(1,m_id);	
		return pstmt.executeQuery();
	    		   
			  
		}catch(Exception e){
			e.printStackTrace();
		}		
		return null;
	
	}

	@Override
	/*-----------这是进入订餐车查询所有--------------*/
	public ArrayList selectCart(int t_id) throws SQLException {
		ArrayList<Carts> list = new ArrayList<Carts>();
		String sql;
		//  select a.m_url_defal , a.m_name , a.m_price , a.m_describe , b.o_number  from  menu a , orders b , desk c where a.m_id=b.m_id and c.t_id=b.t_id and c.t_id=1
		if(t_id==0)//搜全部
		{
			sql ="select b.t_id, a.m_url_defal  , a.m_name , a.m_price , a.m_describe , a.m_id,b.o_number  from  menu a , orders b , desk c where a.m_id=b.m_id and c.t_id=b.t_id";
		}
		else	//搜部分
		{
			sql="select b.t_id, a.m_url_defal , a.m_name , a.m_price , a.m_describe , a.m_id,b.o_number  from  menu a , orders b , desk c where a.m_id=b.m_id and c.t_id=b.t_id and c.t_id="+t_id;
			
		}		
		seach find=new seach();
		ResultSet rs=find.f(sql);
		while(rs!=null&&rs.next())
		{
			Carts pojo=new Carts();			
			pojo.setM_describe(rs.getString("m_describe"));
			pojo.setM_name(rs.getString("m_name"));
			pojo.setM_price(rs.getString("m_price"));
			pojo.setM_url_defal(rs.getString("m_url_defal"));
			pojo.setO_number(rs.getInt("o_number"));
			pojo.setM_id(rs.getInt("m_id"));
			pojo.setT_id(rs.getInt("t_id"));
			
			/*动态增加jsp页面的id*/
			
		
				pojo.setJsp_add(DongTaiDate());
				pojo.setJsp_min(DongTaiDate());				
				pojo.setJsp_zonjia(DongTaiDate());				
				pojo.setJsp_danjia(DongTaiDate());			
				pojo.setJsp_number(DongTaiDate());
			
			
			list.add(pojo);
		}
		
		
		return list;
	}

	@Override
	/*-----------删除订餐内容且更新餐桌状态----------*/
	public int deleteCarts(int m_id,Desk_id t_id) throws SQLException {
		String sql="exec  UpDateDesk "
				+m_id+","
				+Integer.parseInt(t_id.getT_id());
		dele d = new dele();
		int str=d.dele(sql);
		System.out.println("删除时和更新执行的存储过程 : "+sql);
		if(str==1)		
		return 1;
		else{
			return 0;
		}
				
	}

	
  /*-----模糊查询------*/
	/*@SuppressWarnings("unchecked")
	public ArrayList<Menu> query(String strType,String strKey){   //搜索关键字
		  ResultSet rs=null;
		  seach find=new seach();
		  ResultSetMetaData md=null;
		  ArrayList<Menu> arrList = new ArrayList<Menu>();
	try{
		 
		if(strType!=null&&!strType.trim().equals("")){
			 String sql="select * from menu where "+strType+" like "+"'%"+strKey+"%'";
			 System.out.println(sql);
			 rs=find.f(sql);
			 md=rs.getMetaData();                       //得到结果集（rs）的结构信息，比如字段数，字段名
			 int columnCount=md.getColumnCount();		//返回ResultSet对象中 列数	 
			while(rs.next()){
			      Map rowData=new HashMap();
			      for(int i=1;i<=columnCount;i++){       //把结果集的数一个一个取出来
			    	
			    		  rowData.put(md.getColumnName(i), rs.getObject(i));
			    	  
			      }
			      arrList.add((Menu) rowData);			      
			}
			return arrList; 
		 }
		
	
		   	
	}catch(SQLException e){
			  e.printStackTrace();
		     }
		return null;		
	}		*/		
	
	
	
	
	

}
